// 用户任务节点
import {CircleNode, CircleNodeModel, GraphModel, h, LogicFlow} from '@logicflow/core'
import {endIcon} from '../icon'
import {genBpmnId} from "@logicflow/extension/src/bpmn-elements/utils";
import {FlowNodeTypeEnum} from "@/enums/FlowNodeTypeEnum"
import {sourceRulesNotOut} from "./rules/NodeValidateRules";

// 自定义业务节点外观
class view extends CircleNode {
    getIconShape() {
        const {model} = this.props
        const style = model.getNodeStyle()
        const {
            x,
            y,
            width,
            height,
        } = model
        return h(
            'svg',
            {
                x: x - width / 2 + 5,
                y: y - height / 2 + 5,
                width: 50,
                height: 50,
                viewBox: '0 0 1024 1024'
            },
            h(
                'path',
                {
                    fill: '#6692CC',
                    d: endIcon
                }
            )
        )
    }

    getShape() {
        const {model} = this.props
        const {x, y, r} = model
        const {fill, stroke, strokeWidth} = model.getNodeStyle()
        return h('g', {}, [
                h(
                    'circle',
                    {
                        cx: x,
                        cy: y,
                        r,
                        fill,
                        stroke,
                        strokeWidth
                    }
                ),
                h(
                    'text',
                    {
                        fill: '#000000',
                        fontSize: 12,
                        x: x - 12,
                        y: y + 4,
                        width: 50,
                        height: 25,
                    },
                    '结束'
                ),
                this.getIconShape()
            ]
        )
    }

}

class model extends CircleNodeModel {
    constructor(data: NodeConfig, graphModel: GraphModel) {
        if (!data.id) {
            data.id = `end_${genBpmnId()}`
        }
        super(data, graphModel)
    }
    // 自定义节点形状（圆角矩形）
    initNodeData(data: LogicFlow.NodeConfig) {
        super.initNodeData(data);
        // 调整圆的半径
        this.r = 30
        // 不允许文本被编辑
        this.text.editable = false
    }

    //自定义业务节点样式
    getNodeStyle() {
        const style = super.getNodeStyle()
        return style
    }
    // 获取当前节点作为边的起始节点规则
    getConnectedSourceRules(): Model.ConnectRule[] {
        const rules = super.getConnectedSourceRules();
        rules.push(sourceRulesNotOut);
        return rules;
    }
}

// 注册组件
export function register(lf: LogicFlow) {
    lf.register({
        type: FlowNodeTypeEnum.FLOW_END,
        view: view,
        model: model,
    })
}

